package solutions;

import common.LinkNode;
import sun.awt.image.ImageWatched;

/**
 * 反向输出链表
 * Created by Wongy on 2018/3/26.
 */
public class Solution16 {

    private static LinkNode reverseList(LinkNode head) {
        if (head == null) {
            return null;
        }
        if (!head.hasNext()) {
            return head;
        }
        LinkNode pPre = null;
        LinkNode p = head;
        LinkNode pNext = head.next;
        LinkNode newHead = null;
        while (p != null) {
            if (!p.hasNext()) {
                newHead = p;
            }
            pNext = p.next;
            p.next = pPre;
            pPre = p;
            p = pNext;
        }
        return newHead;
    }


    public static void main(String[] args) {
        LinkNode linkNode9 = new LinkNode(9);
        LinkNode linkNode8 = new LinkNode(8, linkNode9);
        LinkNode linkNode7 = new LinkNode(7, linkNode8);
        LinkNode linkNode6 = new LinkNode(6, linkNode7);
        LinkNode linkNode5 = new LinkNode(5, linkNode6);
        LinkNode linkNode4 = new LinkNode(4, linkNode5);
        LinkNode linkNode3 = new LinkNode(3, linkNode4);
        LinkNode linkNode2 = new LinkNode(2, linkNode3);
        LinkNode linkNode1 = new LinkNode(1, linkNode2);
        LinkNode linkNode0 = new LinkNode(0, linkNode1);
        System.out.println(linkNode0);
        System.out.println(reverseList(linkNode4));
        System.out.println(linkNode1);

    }
}
